package com.azkj.album.presenter;

import android.util.Log;
import android.widget.TextView;

import com.azkj.album.dto.AddOrderBean;
import com.azkj.album.dto.HasNickName;
import com.azkj.album.network.ConfigStorage;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.ServiceCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.network.utils.StoreUtils;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.view.base.BasePresenter;
import com.azkj.album.view.iview.IUserView;
import com.azkj.album.view.widgets.dialog.DialogHelper;

import java.util.Map;

public class UserPresenter extends BasePresenter {

    private IUserView iUserView;

    public UserPresenter(IUserView iUserView) {
        this.iUserView = iUserView;
    }

    public void clearUser() {

        NetworkMaster.getInstance().getCommonService().clearUser(new ServiceCallback<BaseResp>() {
            @Override
            public void onSuccess(BaseResp jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iUserView.clearUserSuccess();
                } else {
                    iUserView.clearUserFail(jsonObj.getMsg());
                }

            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iUserView.clearUserFail(apiException.errorInfo.error);
            }

        });


    }

    public void logout() {
        NetworkMaster.getInstance().getCommonService().logout(new ServiceCallback<BaseResp>() {
            @Override
            public void onSuccess(BaseResp jsonObj) {
                StoreUtils.clear();
            }

            @Override
            public void onError(ApiException apiException) {
                StoreUtils.clear();
            }

        });

    }

    public void hasNickName(String name, Map<String, Object> body, TextView textView){
        NetworkMaster.getInstance().getCommonService().hasNickName(name, new ServiceCallback<BaseResp<HasNickName>>() {
            @Override
            public void onSuccess(BaseResp<HasNickName> jsonObj) {
                if (jsonObj.getCode() == 1){
                    if ("1".equals(jsonObj.getData().getCan())){
                        iUserView.hasNickName(true, name, body, textView);
                    }else {
                        iUserView.hasNickName(false, name, body, textView);
                    }
                }
            }

            @Override
            public void onError(ApiException apiException) {
                iUserView.hasNickName(false, name, body, textView);
            }
        });
    }

    public void hasNickName(String name){
        hasNickName(name, null, null);
    }

    public void changenickName(String name){
        NetworkMaster.getInstance().getCommonService().changenickName(ConfigStorage.getInstance().getToken(), name, new ServiceCallback<BaseResp>() {
            @Override
            public void onSuccess(BaseResp jsonObj) {
                if (jsonObj.getCode() == 1){
                    iUserView.changeNameSuccess(true, name);
                }else {
                    iUserView.changeNameSuccess(false, name);
                    ToastUtils.showCenterToast(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {

            }
        });
    }

}
